package com.com.effectivejava.objectcreation;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Created by pankaj on 7/6/14.
 */
public class ServiceAndItsProvider {
    // Service provider framework sketch


}
      //Service Interface.
      interface Service{
        // service specific methods go here.
      }

      // Service Provider Interface.
      interface Provider{
        Service newService();
      }

      // Non instantiable class for service registration and access.
       class Services {
          // prevents instantiation.
          private Services() {
          }

          //Maps service names to services.
          private static final Map<String, Provider> providers =
                  new ConcurrentHashMap<String, Provider>();

          public static final String DEFAULT_PROVIDER_NAME = "<DEFAULT>";

          //provider registration API
          public static void registerDefaultProvider(Provider p){
              registerProvider("<DEFAULT>", p);
          }

          public static void registerProvider(String name, Provider p){
              providers.put(name, p);
          }

          //Service Access API
          public static Service newInstance(){
              return newInstance("<DEFAULT>");
          }

          public static Service newInstance(String name){
              Provider p = providers.get(name);

              return p.newService();
          }

      }






